package com.jandar.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

import com.jandar.util.SecurityUtils;

/**
 * 文本加密对话框。
 * 
 * @author pqj
 * @version 2009-7-31
 */
public class EncoderDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel label = null;

	private JPanel mainPanel = null;

	private JPanel cmdPanel = null;

	private JButton encoderBut = null;

	private JScrollPane inputScrollPane = null;

	private JScrollPane outputScrollPane = null;

	private JTextArea outputTextArea = null;

	private JPanel infoPanel = null;

	private JTextArea inputTextArea = null;

	/**
	 * @param owner
	 */
	public EncoderDialog(Frame owner) {
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(400, 350);
		this.setTitle("加密工具");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setVgap(2);
			label = new JLabel();
			label.setText("<html><b>提示：</b><br>1）将配置文件中的JDBC链接的URL信息拷贝至输入文本框内，然后点击加密文本按钮，将输出文本拷贝并覆盖配置文件中的URL。<br>2）重新运行程序。</html>");
			label.setForeground(Color.red);
			jContentPane = new JPanel();
			jContentPane.setLayout(borderLayout);
			jContentPane.add(getMainPanel(), BorderLayout.CENTER);
			jContentPane.add(getCmdPanel(), BorderLayout.SOUTH);
			jContentPane.add(getInfoPanel(), BorderLayout.NORTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes mainPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getMainPanel() {
		if (mainPanel == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(2);
			gridLayout.setColumns(1);
			mainPanel = new JPanel();
			mainPanel.setLayout(gridLayout);
			mainPanel.add(getInputScrollPane(), null);
			mainPanel.add(getOutputScrollPane(), null);
		}
		return mainPanel;
	}

	/**
	 * This method initializes cmdPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getCmdPanel() {
		if (cmdPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(java.awt.FlowLayout.RIGHT);
			cmdPanel = new JPanel();
			cmdPanel.setLayout(flowLayout);
			cmdPanel.add(getEncoderBut(), null);
		}
		return cmdPanel;
	}

	/**
	 * This method initializes encoderBut	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getEncoderBut() {
		if (encoderBut == null) {
			encoderBut = new JButton();
			encoderBut.setText("加密文本");
			encoderBut.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String encoderText = SecurityUtils.base64Encoder(inputTextArea.getText());
					outputTextArea.setText(encoderText);
				}
			});
		}
		return encoderBut;
	}

	/**
	 * This method initializes inputScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getInputScrollPane() {
		if (inputScrollPane == null) {
			inputScrollPane = new JScrollPane();
			inputScrollPane.setBorder(BorderFactory.createTitledBorder(null, "\u8f93\u5165\u6587\u672c", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			inputScrollPane.setViewportView(getInputTextArea());
		}
		return inputScrollPane;
	}

	/**
	 * This method initializes outputScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getOutputScrollPane() {
		if (outputScrollPane == null) {
			outputScrollPane = new JScrollPane();
			outputScrollPane.setBorder(BorderFactory.createTitledBorder(null, "\u8f93\u51fa\u6587\u672c", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			outputScrollPane.setViewportView(getOutputTextArea());
		}
		return outputScrollPane;
	}

	/**
	 * This method initializes outputTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getOutputTextArea() {
		if (outputTextArea == null) {
			outputTextArea = new JTextArea();
			outputTextArea.setLineWrap(true);
			outputTextArea.setToolTipText("加密后的文本");
		}
		return outputTextArea;
	}

	/**
	 * This method initializes infoPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getInfoPanel() {
		if (infoPanel == null) {
			infoPanel = new JPanel();
			infoPanel.setLayout(new BorderLayout());
			infoPanel.add(label, BorderLayout.CENTER);
		}
		return infoPanel;
	}

	/**
	 * This method initializes inputTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getInputTextArea() {
		if (inputTextArea == null) {
			inputTextArea = new JTextArea();
			inputTextArea.setLineWrap(true);
			inputTextArea.setToolTipText("输入要加密的文本");
		}
		return inputTextArea;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
